package com.hdoit.ares.logical.official.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.official.dao.IArticalCommentDao;
import com.hdoit.ares.logical.official.mapper.ArticalCommentMapper;
import com.hdoit.ares.logical.official.model.ArticalComment;
import com.hdoit.ares.logical.official.model.ArticalPraise;
import com.hdoit.ares.logical.official.model.PraiseAndGlance;
import com.hdoit.ares.logical.viewmodel.official.ArticalCommentSearch;
import com.hdoit.ares.selfcommon.enums.official.TypeOfPraise;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.utils.FinderExpressionFactory;
/**
 * @author      yujinhui
 * @email       
 * @createtime  2018年03月10日 14:05:06
 * @comment
 */
@Component
public class ArticalCommentDaoImpl extends SuperDao<ArticalComment> implements IArticalCommentDao{

	private ArticalCommentMapper articalCommentMapper;
	public ArticalCommentMapper getArticalCommentMapper() {
		return articalCommentMapper;
	}
	@Resource
	public void setArticalCommentMapper(ArticalCommentMapper articalCommentMapper) {
		this.articalCommentMapper = articalCommentMapper;
	}

	@Override
	public List<ArticalComment> queryListBySearch(ArticalCommentSearch search) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
		finderExpression.select("articalComment.*","praiseAndGlance.praiseCount as praiseCount","praiseAndGlance.glanceCount as glanceCount"
						,"c1.username as customerName","c1.nickname as nickname",
						"c2.username as replyedName","c2.nickname as replyedNickName",
						"c1.picPath as customerImg","articalPraise.customerId as praiseCustomerId")
						.from(ArticalComment.class,"articalComment")
						.leftJoin(PraiseAndGlance.class, "praiseAndGlance")
						.on("articalComment.id = praiseAndGlance.receiveId")
						.leftJoin(Customer.class,"c1")
						.on("c1.id = articalComment.customerId")
						.leftJoin(Customer.class,"c2")
						.on("c2.id = articalComment.replyedId")
						.leftJoin(ArticalPraise.class,"articalPraise")
						.on("articalComment.id = articalPraise.receiveId and articalPraise.customerId = #{praiseCustomerId}")
						.setParam("praiseCustomerId", search.getPraiseCutomerId())
						.andWhere("praiseAndGlance.type = #{type}")
						.setParam("type", TypeOfPraise.Comment)
						.andWhere("articalComment.deleteMark = #{deleteMark}")
						.setParam("deleteMark", YesOrNo.no)
						.addOrder("articalComment.createTime", OrderEnum.desc);
		
		appendFinder(finderExpression, search);
		
		return queryList(finderExpression);
	}

	private void appendFinder(IFinderExpression finderExpression, ArticalCommentSearch search) {
		
		if(!StringUtils.isEmpty(search.getId())){
			finderExpression.andWhere("articalComment.id = #{id}")
							.setParam("id", search.getId());	
		}
		
		if(!StringUtils.isEmpty(search.getArticalId())){
			finderExpression.andWhere("articalComment.articalId = #{articalId}")
							.setParam("articalId", search.getArticalId());	
		}
		
		if(!StringUtils.isEmpty(search.getCustomerId())){
			finderExpression.andWhere("articalComment.customerId = #{customerId}")
							.setParam("customerId", search.getCustomerId());	
		}
		

		if(search.getBeforeTime()!=null){

			finderExpression.andWhere("TIMESTAMPDIFF(SECOND,articalComment.createTime,#{beforeTime})>0")
				  			.setParam("beforeTime", search.getBeforeTime());
		}
		
		
		if(search.getPageNo()!=null){
			finderExpression.limit((search.getPageNo() - 1)*search.getPageSize(), search.getPageSize());
		}else{
			if(search.getPageSize()!=null){
				finderExpression.limit(0, search.getPageSize());
			}
		}
	}

	@Override
	public ArticalComment queryModel(ArticalCommentSearch search) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
		finderExpression.select("articalComment.*","praiseAndGlance.praiseCount as praiseCount","praiseAndGlance.glanceCount as glanceCount"
						,"c1.username as customerName","c1.nickname as nickname",
						"c2.username as replyedName","c2.nickname as replyedNickName",
						"c1.picPath as customerImg","articalPraise.customerId as praiseCustomerId")
						.from(ArticalComment.class,"articalComment")
						.leftJoin(PraiseAndGlance.class, "praiseAndGlance")
						.on("articalComment.id = praiseAndGlance.receiveId")
						.leftJoin(Customer.class,"c1")
						.on("c1.id = articalComment.customerId")
						.leftJoin(Customer.class,"c2")
						.on("c2.id = articalComment.replyedId")
						.leftJoin(ArticalPraise.class,"articalPraise")
						.on("articalComment.id = articalPraise.receiveId and articalPraise.customerId = #{praiseCustomerId}")
						.setParam("praiseCustomerId", search.getPraiseCutomerId())
						.andWhere("praiseAndGlance.type = #{type}")
						.setParam("type", TypeOfPraise.Comment)
						.andWhere("articalComment.deleteMark = #{deleteMark}")
						.setParam("deleteMark", YesOrNo.no)
						.addOrder("articalComment.createTime", OrderEnum.desc);
		
		appendFinder(finderExpression, search);
		
		return queryModel(finderExpression);
	}

	@Override
	public int queryCountDayComments(String month, String day, String customerId) {
		return articalCommentMapper.queryCountDayComments(month, day,customerId);
	}

	
	
}
